package com.zhentao.product.controller;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@RestController
@RequestMapping("sys")
public class AliyunSmsutil {

    // 阿里云账号
    private static final String accessKeySecret = "WlkuvAOEZWDJitk9UrpXhmwBOchwCb";
    // 阿里云账号密码
    private static final String accessKeyId = "LTAI5tAANoB74wiRygvsZKz3";
    //区域
    private static final String regionId = "cn-hangzhou";


    @GetMapping("send")
    public static boolean sendSms(String phone){
        boolean flag = false;
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "开始车检");
        request.putQueryParameter("TemplateCode", "SMS_474085365");
        Date now = new Date();
        //定义日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日，EEEE，hh'点'", Locale.CHINA);
        //格式化日期
        String format = dateFormat.format(now);
        //输出结果
        System.out.println("当前日期格式化后为："+format);
        //把时间转成JSON格式
        request.putQueryParameter("TemplateParam", "{\"address\":\"保定\",\"time\":\""+format+"\"}");
        try{
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            flag = true;
        }catch (ServerException e){
            e.printStackTrace();
        }catch (ClientException e){
            e.printStackTrace();
        }
        return flag;
    }
}
